package com.lc.afbx.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiMsg;
import com.lc.afbx.conn.PostApiRePassword;
import com.lc.afbx.util.Validator;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilNetWord;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/5/31.
 */
public class LosePasswordActivity extends BaseActivity implements View.OnClickListener {


    private TextView lose_password_get_code_tv;
    private EditText lose_password_phone_et;
    private EditText lose_password_code_et;
    private EditText lose_password_new_password_et;
    private EditText lose_password_confirm_password_et;

    //网络请求获取的验证码
    private String getCode;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            lose_password_get_code_tv.setText(millisUntilFinished / 1000
                    + "s重新获取");
//                            time.setTextColor(getResources().getColor(R.color.white));
//                            time.setBackgroundColor(getResources().getColor(R.color.title_theme_color));
        }

        @Override
        public void onFinish() {
            lose_password_get_code_tv.setText("重新获取");
//                            time.setTextColor(getResources().getColor(R.color.bingdingtel_reget_code_text_color));
//                            time.setBackgroundColor(getResources().getColor(R.color.bingdingtel_reget_code_bg_color));
            lose_password_get_code_tv.setEnabled(true);

        }
    };

    private PostApiRePassword postApiRePassword = new PostApiRePassword(new AsyCallBack<PostApiRePassword.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiRePassword.Info info) throws Exception {

            UtilToast.show(context, toast);
            LosePasswordActivity.this.finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    private GetApiMsg getApiMsg = new GetApiMsg(new AsyCallBack<GetApiMsg.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiMsg.Info info) throws Exception {

            UtilToast.show(context, toast);

            getCode = info.getcode;

            countTime.start();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
            countTime.cancel();//取消计时
            lose_password_get_code_tv.setText("获取验证码");
            lose_password_get_code_tv.setEnabled(true);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lose_password);

        lose_password_get_code_tv = ((TextView) findViewById(R.id.lose_password_get_code_tv));
        lose_password_phone_et = ((EditText) findViewById(R.id.lose_password_phone_et));
        lose_password_code_et = ((EditText) findViewById(R.id.lose_password_code_et));
        lose_password_new_password_et = ((EditText) findViewById(R.id.lose_password_new_password_et));
        lose_password_confirm_password_et = ((EditText) findViewById(R.id.lose_password_confirm_password_et));

        findViewById(R.id.lose_password_submit_bt).setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(this);
        lose_password_get_code_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.lose_password_submit_bt:

                if (lose_password_phone_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "账号不能为空");
                    return;
                }

                if (!Validator.isMobile(lose_password_phone_et.getText().toString())) {
                    UtilToast.show(context, "手机号格式错误");
                    return;
                }

                if (lose_password_code_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "验证码不能为空");
                    return;
                }

                if (!(lose_password_code_et.getText().toString().equals(getCode))) {

                    if (getCode != null) {

                        UtilLog.e("===========", getCode + "    " + lose_password_code_et.getText().toString().trim());

                        UtilToast.show(this, "验证码错误");

                    } else {

                        UtilToast.show(this, "请点击获取验证码");

                    }
                    return;
                }

                if (lose_password_new_password_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "密码不能为空");
                    return;
                }

                if (lose_password_confirm_password_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "确认密码不能为空");
                    return;
                }

                if (lose_password_new_password_et.getText().toString().length() < 6) {
                    UtilToast.show(this, "密码最少6位");
                    return;
                }

                if (!lose_password_confirm_password_et.getText().toString().equals(lose_password_new_password_et.getText().toString())) {
                    UtilToast.show(context, "两次输入密码不一致");
                    return;
                }

                postApiRePassword.username = lose_password_phone_et.getText().toString();
                postApiRePassword.user_pass_now = lose_password_confirm_password_et.getText().toString().trim();
                postApiRePassword.execute(this);
                return;

            case R.id.lose_password_get_code_tv:

                if (lose_password_phone_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "请输入手机号");
                    return;
                }

                if (!Validator.isMobile(lose_password_phone_et.getText().toString())) {
                    UtilToast.show(context, "手机号格式错误");
                    return;
                }
                if (UtilNetWord.isNetworkConnected(context)) {

                    lose_password_get_code_tv.setEnabled(false);
                    //获取验证码接口
                    getApiMsg.username = lose_password_phone_et.getText().toString();
                    getApiMsg.type = "find";

                    getApiMsg.execute(this);

                } else {
                    UtilToast.show(context, "没有网");
                }

                return;

        }


    }


}
